<style lang="less" scoped>
  .acpt-list-body {
    .txt {
      padding-right:0 !important;
    }
  }
  .mb30 {
    &:last-of-type{
      margin: 0;
    }
  }
</style>

<template>
  <div>
    <div class="m-sech-wrap mr20">
      <router-link :to="{path:'/search',query: { type: 'settle', tabNo: this.tabNo}}" class="m-sech-box displayflex">
        <i class="iconfont icon-search"></i>
        <div class="flex1 m-sech-ipt">
          <div class="txt">{{sechTxt || '结算单号/单位'}}</div>
        </div>
      </router-link>
    </div>

    <div class="tab-nav">
      <div class="displayflex">
        <div class="flex1" v-bind:class="{selected:tabNo===1}" @click="changeTab(1)">未录发票</div>
        <div class="flex1" v-bind:class="{selected:tabNo===2}" @click="changeTab(2)">已录发票</div>
      </div>
    </div>

    <scroller lock-x scrollbar-y use-pullup height="-168" v-if="todolist.length > 0" ref="Scroller" :pullup-config="ScrollerConfig" @on-pullup-loading="loadMore">
      <div class="acpt-list">
        <div class="acpt-list-wrap mb30"  @click="goToDetail(item.Guid)" v-for="(item, index) in todolist">
          <div class="acpt-list-head cf">
            <span class="time f-l">结算单号：{{item.SettleBillNo}}</span>
            <em class="org f-r ft12" v-if="item.InvoiceState === 'I01'" >未录发票</em>
            <em class="org f-r ft12" v-if="item.InvoiceState === 'I02'" >已录发票</em>
          </div>
          <div class="displayflex acpt-list-body">
            <i class="iconfont icon-cargo"></i>
            <div class="txt flex1">
              <p>结算金额：￥{{item.Amount.toFixed(2)}}</p>
              <p>结算时间：{{item.SettleDate | timeshort}}</p>
              <p>结算单位：{{item.SettleCorpName}}</p>
              <p>结算周期：{{item.BeginDate | time}} - {{item.EndDate | time}}</p>
            </div>
          </div>
        </div>
      </div>
      <div v-if="noMore" class="loader-over">没有更多记录了</div>
    </scroller>

    <div class="noDate_page" v-if="todolist.length <= 0">
      <div class="noDate_icon"><i class="iconfont icon-null"></i></div>
      暂无数据！
    </div>
    <manage-footer :status='2'></manage-footer>
  </div>
</template>

<script>
import manageFooter from '@/components/core/footer/manage.footer.vue';

export default {
  components:{
    manageFooter,
  },
  data() {
    return {
      sechTxt: '',
      todolist:[],
      totalRecords:0,
      PageIndex:1,
      PageSize:4,
      noMore:false,
      ScrollerConfig: {
        content: '上拉加载',
        downContent: '上拉加载',
        height: 0,
        loadingContent: '加载中...',
        upContent: '上拉加载',
      },
      tabNo:0,
    };
  },
  watch: {
    tabNo(n,o){
      if(n !== o){
        this.todolist = [];
        this.PageIndex = 1;
        this.noMore = false;
        this.getData();
      }
    }
  },
  computed: {
    userinfo(){
      return localStorage.getItem('userinfo') === null?null:JSON.parse(localStorage.getItem('userinfo'));
    }
  },
  created(){
    this.$nextTick(()=>{
      this.sechTxt = sessionStorage.sechtxt;
      if(sessionStorage.getItem('SettleTab')){
        this.tabNo = sessionStorage.getItem('SettleTab')*1;
        sessionStorage.removeItem('SettleTab')
      } else {
        this.tabNo = 1;
      }
    })
  },
  methods: {
    changeTab(tabNo){
      this.tabNo = tabNo;
    },
    loadMore(){
      this.getData();
    },
    getData(){
      let query = {
            InvoiceState : "I01",
            KeyWord : this.sechTxt,
            TenantGuid : this.userinfo.TenantGuid,
            PageIndex: this.PageIndex,
            PageSize: this.PageSize,
          };
      if(this.tabNo === 2) {
        query.InvoiceState = "";
        query.InvoiceStateList = ['I02', 'I03'];
      }

      this.Ajax.post('getSettleList',query).then((resp) => {
        if (resp.IsSuccess) {
          this.totalRecords = resp.TotalRecords;
          if(resp.Datas !== null && resp.Datas.length > 0){
            this.PageIndex++;
            this.todolist = this.todolist.concat(resp.Datas);
            this.ScrollControl();
          }
          
        } else {
          this.Toast({
            message: resp.ErrorMessage || '操作异常，请稍后重试',
          });
        }
      });
    },
    goToDetail(Guid){
      this.$router.push({
        path: '/settle/Detail',
        query: { Guid: Guid }
      })
    },

    ScrollControl(){
      if(this.totalRecords <= this.PageSize){
        this.ScrollerConfig.content = '';
        this.noMore = true;
        this.$nextTick(() => {
          this.$nextTick(() => {
            this.$refs.Scroller.disablePullup();
          });
        });
      }

      if(this.todolist.length > 0 && this.$refs.Scroller){
        this.$nextTick(() => {
          this.$nextTick(() => {
            this.$refs.Scroller.donePullup();
          });
        });
      }

      if (this.totalRecords == this.todolist.length) {
        this.$nextTick(() => {
          this.$nextTick(() => {
            this.$refs.Scroller.disablePullup();
            this.noMore = true;
          });
        });
      }
    }
  },
}
</script>
